﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Linq;
using KLS.PPDS.Data.Database.Common;


namespace KLS.PPDS.Data.Database.Repository
{
    public class LinqRepository<T> : ILinqRepository<T>, IDisposable
    {
        //public ISession session
        //{
        //    get
        //    {
        //        return NHibernateSessionManager.Instance.GetSession();
        //    }
        //}

       public ISession Session { get; set; }

        //public LinqRepository()
        //{           
        //    session = NHibernateHelper.GetCurrentSession();
        //}

        public LinqRepository()
        {
            Session = NHibernateSessionManager.Instance.GetSession();
        }




        public void Delete(T target)
        {
            Session.Delete(target);
        }

        public T FindOne(int id)
        {
            return Session.Get<T>(id);
        }

        public T FindOne(ILinqSpecification<T> specification)
        {
            return specification.SatisfyingElementsFrom(Session.Linq<T>()).SingleOrDefault();
        }

        public T FindOne(ILinqSpecification<T, T> specification)
        {
            return specification.SatisfyingElementsFrom(Session.Linq<T>()).SingleOrDefault();
        }

        public IQueryable<T> FindAll()
        {

            return Session.Linq<T>().AsQueryable();

        }

        public IQueryable<T> FindAll(ILinqSpecification<T> specification)
        {
            return specification.SatisfyingElementsFrom(Session.Linq<T>());
        }

        public IQueryable<T> FindAll(ILinqSpecification<T, T> specification)
        {
            return specification.SatisfyingElementsFrom(Session.Linq<T>());
        }

        public void Save(T entity)
        {
            try
            {
                Session.Save(entity);
            }
            catch
            {
                if (Session.IsOpen)
                {
                    Session.Close();
                }

                throw;
            }

            Session.Flush();
        }

        public void SaveAndEvict(T entity)
        {
            this.Save(entity);
            Session.Evict(entity);
        }


        public void Update(T entity)
        {
            try
            {
               
                Session.Update(entity);
            }
            catch
            {
                if (Session.IsOpen)
                {
                    Session.Close();
                }

                throw;
            }

            Session.Flush();



        }
        public void Flush()
        {
            Session.Flush();
        }

        public IList<T> FindAllWithSort(string propertyName, string orderDirection)
        {
            string query = string.Format(@"from {0} obj order by obj.{1} {2}", typeof(T).ToString(), propertyName, orderDirection);
            return Session.CreateQuery(query).List<T>();
        }

        /// <summary>
        /// Clear NHibernet session
        /// </summary>
        public void Clear()
        {
            Session.Clear();
        }

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            Session.Clear();
            Session.Close();
        }

        #endregion
    }
}
